# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import InterfaceUtils
import AdvancedShareWidget

class NewCategoryDialog:
	def __init__(self, parentWindow, account, addCategoryCb):
		self.wTree = InterfaceUtils.GetGladeTree("NewCategoryDialog")
		
		signals = {
			"OnOk"			: self.OnOk,
			"OnCancel"		: self.OnCancel,
			"OnClose"		: self.OnCancel
		}
		
		self.wTree.signal_autoconnect(signals)
		
		self.account = account
		self.addCategoryCb = addCategoryCb
		
		self.window = self.wTree.get_widget("NewCategoryDialog")
		self.window.set_transient_for(parentWindow)
		self.window.set_modal(True)
		
		self.paidByCombo = self.wTree.get_widget("CategoryPaidByCombo")
		self.nameEntry = self.wTree.get_widget("CategoryNameEntry")
		self.sharedByAlignment = self.wTree.get_widget("SharedByAlignment")
		self.shareWidget = AdvancedShareWidget.AdvancedShareWidget(self.account, self.window)
		self.sharedByAlignment.add(self.shareWidget)
		
		self.paidByCombo.append_text("Anyone")
		self.paidByCombo.set_active(0)
		for person in self.account.getPeople():
			self.paidByCombo.append_text(person.getName())
			
		self.window.show_all()
		
	def OnOk(self, widget):
		name = self.nameEntry.get_text()
		paid = self.paidByCombo.get_active_text()
		shareProfile = self.shareWidget.GetProfile()
		
		msg = None
		if not name:
			msg = "You must provide a name for the category."
		elif self.account.categoryExists(name):
			msg = "A category with that name already exists.\nPlease choose a unique name."
		elif len(shareProfile) == 0:
			msg = "Please select at least one person to share this category."
		
		if msg:
			InterfaceUtils.ShowErrorDialog(self.window, msg)
			return
		
		if paid == "Anyone":
			paid = None
		
		self.addCategoryCb(name, paid, shareProfile)
		
		self.window.destroy()
	
	def OnCancel(self, widget):
		self.window.destroy()
